package com.jiao.spring_es.controller;

import com.jiao.spring_es.client.ElasticSearchClientConnect;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

@RestController
public class ElasticsearchController {

    @Autowired
    private ElasticSearchClientConnect elasticSearchClientConfig;

    /**
     * 新建my_index索引
     * @return
     * @throws IOException
     */
    @GetMapping("/createIndex")
    public Boolean createIndex() throws IOException {

        RestHighLevelClient client = elasticSearchClientConfig.restClient();
        CreateIndexRequest request = new CreateIndexRequest("jing_index");
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);

        // 打印结果
        System.out.println(createIndexResponse.isAcknowledged());

        return createIndexResponse.isAcknowledged();
    }

    /**
     * 查询索引
     *
     * @return
     * @throws IOException
     */
    @GetMapping("/selectIndex")
    public Map<String, Set<String>> selectIndex() throws IOException {
        RestHighLevelClient client = elasticSearchClientConfig.restClient();
        GetIndexRequest request = new GetIndexRequest("jing_index");
        GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);

        // 打印结果
        System.out.println("getIndexResponse.getAliases() = " + getIndexResponse.getAliases().keySet());
        System.out.println("getIndexResponse.getMappings() = " + getIndexResponse.getMappings().keySet());
        System.out.println("getIndexResponse.getSettings() = " + getIndexResponse.getSettings().keySet());

        // 返回结果
        return Map.of(
                "aliases", getIndexResponse.getAliases().keySet(),
                "mappings", getIndexResponse.getMappings().keySet(),
                "settings", getIndexResponse.getSettings().keySet()
        );
    }

    /**
     * 删除索引
     * @return
     * @throws IOException
     */
    @GetMapping("/deleteIndex")
    public Boolean deleteIndex() throws IOException {
        // 删除索引
        RestHighLevelClient client = elasticSearchClientConfig.restClient();
        DeleteIndexRequest request = new DeleteIndexRequest("jing_index");
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);

        System.out.println("删除操作 = " + delete);

        return delete.isAcknowledged();
    }


}
